package com.server.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.password.MyMD5;
import com.server.service.ICommonDataService;
import com.server.service.IMngUserService;
import com.server.service.ISMSCenterService;
import com.server.service.impl.SMSCenterService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller("DispatchController")
@RequestMapping("dispatch")
public class DispatchController {
	
	private String tableName = "education.dispatch";
	
	@Autowired
	private IMngUserService mngUserService;
	@Autowired
	private ICommonDataService commonDataService;
	@Autowired
	private ISMSCenterService smsCenterService;
	
	@Login
	@RequestMapping("")
	public ModelAndView dispatch(){
		ModelAndView mv = new ModelAndView("dispatch/dispatchList");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		return mv;
	}
	@Login
	@RequestMapping("list")
	public void list(HttpServletRequest request,
			HttpServletResponse response) {
		String _whereStr = "valid=1";
		String sortStr = "createTime desc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(tableName, _whereStr, sortStr, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@Login
	@RequestMapping("add/{type}")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/dispatch/dispatchForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "new");
		mv.addObject("type", type);
		mv.addObject("business", "dispatch");
		mv.addObject("titleName", "新增调度");
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("save/{id}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id){
		String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String formData = request.getParameter("formData");
		Map<String, Object> saveMap = JSONSerielizer.toMap(formData);
		Boolean isSuccess = true;
		if (id.equals(0)) {
			String time = saveMap.get("time1").toString().substring(0, 10)+saveMap.get("time2").toString();
			saveMap.put("time",time);
			saveMap.remove("time1");
			saveMap.remove("time2");
			if(!StringUtils.isEmpty(saveMap.get("leuuids").toString())){
				Integer ids = mngUserService.update(saveMap.get("leuuids").toString());
			}
			if(!StringUtils.isEmpty(saveMap.get("luuids").toString())){
				Integer ids = mngUserService.update(saveMap.get("luuids").toString());
			}
			if(!StringUtils.isEmpty(saveMap.get("fuuids").toString())){
				Integer ids = mngUserService.update(saveMap.get("fuuids").toString());
			}
			isSuccess = commonDataService.add(operator, tableName, saveMap);
			String phoneNos = saveMap.get("lephoneNos").toString()+","+saveMap.get("tphoneNos").toString()+","+saveMap.get("lphoneNos").toString();
			String startTime =saveMap.get("time").toString();
			String place = saveMap.get("field").toString();
			String action = saveMap.get("title").toString();
			String fphoneNos = saveMap.get("fphoneNos").toString();
			if(isSuccess){
				smsCenterService.dispatchSend(phoneNos, "用户", startTime, place, action);
				smsCenterService.needPlaceSend(fphoneNos, place+"联系人", startTime, place, action);
			}
		}else {
			String time = saveMap.get("time1").toString().substring(0, 10)+saveMap.get("time2").toString();
			saveMap.put("time",time);
			saveMap.remove("time1");
			saveMap.remove("time2");
			if(!StringUtils.isEmpty(saveMap.get("leuuids").toString())){
				Integer ids = mngUserService.update(saveMap.get("leuuids").toString());
			}
			if(!StringUtils.isEmpty(saveMap.get("luuids").toString())){
				Integer ids = mngUserService.update(saveMap.get("luuids").toString());
			}
			if(!StringUtils.isEmpty(saveMap.get("fuuids").toString())){
				Integer ids = mngUserService.update(saveMap.get("fuuids").toString());
			}
			isSuccess = commonDataService.update(operator, tableName, "id", id.toString(), saveMap);
			String phoneNos = saveMap.get("lephoneNos").toString()+","+saveMap.get("tphoneNos").toString()+","+saveMap.get("lphoneNos").toString();
			String startTime =saveMap.get("time").toString();
			String place = saveMap.get("field").toString();
			String action = saveMap.get("title").toString();
			String fphoneNos = saveMap.get("fphoneNos").toString();
			if(isSuccess){
				smsCenterService.dispatchSend(phoneNos, "用户", startTime, place, action);
				smsCenterService.needPlaceSend(fphoneNos, place+"联系人", startTime, place, action);
				
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	@Login
	@RequestMapping("form/{id}")
	public void form(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String id){
		Map<String, Object> formData = commonDataService.getFormData(tableName, "id", id, null, null);
		OutputUtil.returnString(response, JSONSerielizer.toJson(formData, null, null));
	}
	@Login
	@RequestMapping("check/{id}/{state}/{type}")
	public ModelAndView check(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/dispatch/dispatchForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "check");
		mv.addObject("state", state);
		mv.addObject("type", type);
		mv.addObject("business", "dispatch");
		mv.addObject("titleName", "查看调度");
		mv.addObject("userName", userName);
		return mv;
	}
	@Login
	@RequestMapping("update/{id}/{state}/{type}")
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/dispatch/dispatchForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "edit");
		mv.addObject("type", type);
		mv.addObject("state", state);
		mv.addObject("business", "dispatch");
		mv.addObject("titleName", "编辑调度信息");
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		String ids = request.getParameter("ids");
		Integer estimate = mngUserService.deleteD(ids);
		OutputUtil.returnString(response, estimate.toString());
	}
}
